package linkedlist;

/**
 * Created by JiaJia on 2019/6/29 0029.
 */
public class ReverseLinkedList {
    private static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
    private static void printList(ListNode head){
        while (head!=null){
            System.out.print(head.val+"->");
            head=head.next;
        }
        System.out.println("null");
    }
    private static ListNode reverseList1(ListNode head){
        if (head==null||head.next==null){
            return head;
        }
        ListNode next=head.next;
        ListNode newHead=reverseList1(next);
        next.next=head;
        head.next=null;
        return newHead;
    }
    private static ListNode reverseList2(ListNode head){
        ListNode newHead=new ListNode(-1);
        while (head!=null){
            ListNode next=head.next;
            head.next=newHead.next;
            newHead.next=head;
            head=next;
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;

        printList(n1);
        n1=reverseList1(n1);
        printList(n1);
        n1=reverseList2(n1);
        printList(n1);
    }
}